package cn.com.demo.config;

import cn.com.demo.util.LocalCache;
import cn.com.demo.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyLogoutHandler implements LogoutHandler {
    @Autowired
    private MyTokenConfig myTokenConfig;
    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        String token = request.getHeader(myTokenConfig.getTokenConfigProperties().getHeadName());
        if(!StringUtils.isEmpty(token)) {
            // token清理工作，如在redis中删除token，防止未过期的token继续使用
            String userName = myTokenConfig.getUserName(token);
            LocalCache.remove(userName);
        }
        ResponseUtil.ok(response);
    }
}
